package com.gjy.leetcode;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 2042. 检查句子中的数字是否递增
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-03 15:41:32
 */
public class A2042 {

    @Test
    public void test1() {
        String s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles";
        boolean b = areNumbersAscending(s);
        assertTrue(b);
    }

    @Test
    public void test2() {
        String s = "hello world 5 x 5";
        boolean b = areNumbersAscending(s);
        assertFalse(b);
    }

    @Test
    public void test3() {
        String s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s";
        boolean b = areNumbersAscending(s);
        assertFalse(b);
    }

    @Test
    public void test4() {
        String s = "4 5 11 26";
        boolean b = areNumbersAscending(s);
        assertTrue(b);
    }

    public boolean areNumbersAscending(String s) {
        int preNum = -1;
        for (String token : s.split(" ")) {
            try {
                int curNum = Integer.parseInt(token);
                if (curNum <= preNum) {
                    return false;
                }
                preNum = curNum;
            } catch (Exception ignored) {
            }
        }
        return true;
    }
}
